package cjgl.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cjgl.entity.Teacher;

public class TeacherDAO {
	public Teacher findById(Integer id) {
		Teacher tea = null;
		String sql ="select * from teacher t where t.id="+id;
		Connection conn = ConnectionFactory.getConnection();
		if(conn!=null) {
		Statement stmt;
		try {
			stmt = conn.createStatement();
			ResultSet rs=stmt.executeQuery(sql);
			if(rs.next()) {
				tea = new Teacher(rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4));
			}
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		}
		return tea;
		
	}
	
	public List<Teacher> findAll(){
		List<Teacher> list = new ArrayList<Teacher>();
		String sql = "select * from teacher";
		Connection conn = ConnectionFactory.getConnection();
		if(conn!=null) {
		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while(rs.next()) {
				Teacher tea=new Teacher(rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4));
				list.add(tea);
			}
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		}
		return list;
		
	}
	
	
	public synchronized void insert(Teacher teacher) {
		String sql="insert into teacher values(null,?,?,?)";
		Connection conn = ConnectionFactory.getConnection();
		if(conn!=null) {
		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, teacher.getName());
			pstmt.setString(2, teacher.getPassword());
			pstmt.setString(3, teacher.getUsername());
			pstmt.execute();
			sql="select * from teacher t order by t.id desc";
			ResultSet rs = pstmt.executeQuery(sql);
			rs.next();
			teacher.setId(rs.getInt(1));
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		}
	}
	
	public void update(Teacher teacher) {
		String sql = "update teacher t set t.name=?,t.password=?,t.username=? where t.id=?";
		Connection conn = ConnectionFactory.getConnection();
		if(conn!=null) {
			PreparedStatement pstmt;
			try {
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, teacher.getName());
				pstmt.setString(2, teacher.getPassword());
				pstmt.setString(3, teacher.getUsername());
				pstmt.setInt(4, teacher.getId());
				pstmt.execute();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	
	public void delete(Integer id) {
		String sql="delete from teacher Where id="+id;
		Connection conn = ConnectionFactory.getConnection();
		if(conn!=null) {
			Statement stmt;
			try {
				stmt = conn.createStatement();
				stmt.execute(sql);
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	

	public Teacher findByUsernameAndPassword(String username, String password) {
		Teacher tea= null;
		String sql="select * from teacher where username=? and password=?";
		Connection conn = ConnectionFactory.getConnection();
		if(conn!=null) {
			PreparedStatement pstmt;
			try {
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, username);
				pstmt.setString(2, password);
				ResultSet rs = pstmt.executeQuery();
				if(rs.next()) {
					tea = new Teacher(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4));
				}
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return tea;
	}
	
}
